package model.scaling;

import java.util.ArrayList;
import java.util.List;

import model.auxillary.Parameter;
import model.auxillary.ParameterCollectionScaling;
import model.auxillary.ParameterListScaling.ParameterType;

//#Neu
/**
* An {@code IndustrialSector} is part of the scaling. It allows calculations of 
* relative energy efficiencies of an industrial sector.
* @author Sebastian Perlwitz
*
*/
public class IndustrialSector extends Scaling implements IIndustrialSector {
	
	/**
	 * The rank of this {@code IndustrialSector}. It is used to give parameters
	 * a unique abbreviation.
	 */
	private int rank;
	
	/**
	 * A List that holds every company that belongs to this industrial sector.
	 */
	private ArrayList<ICompany> companies;
	
	/**
	 * To create a new {@code IndustrialSector}.
	 * @param name the name of the industrial sector.
	 * @param id a unique idea that identifies this industrial sector.
	 * @param rank the rank of this industrial sector.
	 */
	public IndustrialSector(
			String name,
			String id,
			int rank) {
		super(name, id);
		this.rank = rank;
		companies = new ArrayList<ICompany>();
		addParameters();
	}
	
	@Override
 	public void addCompany(ICompany company) {
 		companies.add(company);
 	}
	
	@Override
	public void removeAllCompaniesOfIndustrialSector() {
		companies.clear();
	}
	
	@Override
	public ArrayList<ICompany> getCompanies() {
		return companies;
	}
 	
 	/**
	 * To add the defining parameters of this {@code IndustrialSector}.
     */
	private void addParameters(){
		List<Parameter> newParams = new ArrayList<Parameter>();
		
		newParams.add(new Parameter(
				"Relative Energieeffizienz",
				"REE_Branche," + rank,
				"<html>REE<sub>Branche, " + rank + "</sub></html>",
				"<html>[%]</html>",
				false,
				this));
		parameters.addScalingParameters(
				ParameterType.INDUSTRY_RESULT_PARAMETER, newParams);
	}

	@Override
	public void calculate() {
		
		error = false;
    	aborted = false;
    	
    	double REE_Unternehmen_sum = 0.0;
    	double h = companies.size();
    	
    	for(ICompany company : companies) {
    		double REE_Unternehmen = company.getParameter("REE_Unternehmen",
    				ParameterType.COMPANY_RESULT_PARAMETER).getValue();
    		
    		REE_Unternehmen_sum += REE_Unternehmen; // [%]
    	}
    	
    	double REE_Branche = REE_Unternehmen_sum / h; // [%]
    	
    	getParameter("REE_Branche", 
				ParameterType.INDUSTRY_RESULT_PARAMETER).setValue(REE_Branche);
	}

	@Override
	public boolean aborted() {
	   	return aborted;
    }
	
    @Override
    public boolean getError() {
    	return error;
   	}
    
    @Override
	public Parameter getParameter(String abbreviation, ParameterType type) {

		// An example: To find the parameter 'f_lambda,1' by searching for
		// 'f_lambda'.
		String a = abbreviation + "_" + rank;
		String b = abbreviation + "," + rank;
		Parameter paramA = parameters.getParameter(type, a);
		Parameter paramB = parameters.getParameter(type, b);
		if(paramA == null) {
			return paramB;
		} else {
			return paramA;
		}
	}
	
	@Override
	public List<ParameterCollectionScaling> getResults() {
		List<ParameterCollectionScaling> results =
				new ArrayList<ParameterCollectionScaling>();
		results.add(parameters.getParameterCollection(
				ParameterType.INDUSTRY_RESULT_PARAMETER));
		return results;
	}
	
}
